<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/9/4
 * Time: 9:36
 */

namespace app\admin\controller;


use app\admin\model\Bet;
use app\admin\model\Odds;
use Qiniu\Auth;
use think\Cache;
use think\Config;
use think\Request;
use think\Db;

class Gift extends Admin
{

    /**
     * 礼物列表
     */
    public function gift_index(Request $request){

        $data = Db::name('gift')
            ->order('id desc')
            ->paginate(20);
        return view('',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 删除礼物
     */
    public function gift_delete(Request $request){
        $id     = $request->param('id');
        $result = Db::name('gift')->where(array('id'=>$id))->delete();
        if ($result) {
            $this->success_new('删除成功',url('gift/gift_index'));
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 编辑礼物
     */
    public function gift_edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $result=Db::name('gift')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功',url('gift/gift_index'));
            }else{
                $this->error_new('修改失败');
            }
        }else{
            $id     = $request->param('id');
            $data   = Db::name('gift')->where(array('id'=>$id))->find();
            $qiniu_config = config('qiniu');
            $accessKey    = $qiniu_config['ACCESSKEY'];
            $secretKey    = $qiniu_config['SECRETKEY'];
            $auth         = new Auth($accessKey, $secretKey);
            $token        = $auth->uploadToken($qiniu_config['bucket']);
            $domain        = $qiniu_config['domain'];
            return view('',[
                'data'  =>$data,
                'token' =>$token,
                'domain'=>$domain
            ]);
        }
    }

    /**
     * 添加礼物
     */
    public function gift_add(Request $request){
        if($request->isPost()){
            $data    = $request->post();
            $result=Db::name('gift')->insert($data);
            if ($result) {
                $this->success_new('添加成功',url('gift/gift_index'));
            }else{
                $this->error_new('添加失败');
            }
        }else{
            $qiniu_config = config('qiniu');
            $accessKey    = $qiniu_config['ACCESSKEY'];
            $secretKey    = $qiniu_config['SECRETKEY'];
            $auth         = new Auth($accessKey, $secretKey);
            $token        = $auth->uploadToken($qiniu_config['bucket']);
            $domain       = $qiniu_config['domain'];
            return view('',[
                'token'=>$token,
                'domain'=>$domain
            ]);
        }
    }


    /**
     * 音乐列表
     */
    public function music_index(Request $request){
        $data = Db::name('music')
            ->order('id desc')
            ->paginate(20);
        return view('',[
            'page'=>$data->render(),
            'data'=>$data,
        ]);
    }

    /**
     * 删除音乐
     */
    public function music_delete(Request $request){
        $id     = $request->param('id');
        $result   = Db::name('article')->where(array('id'=>$id))->delete();
        if ($result) {
            $this->success_new('删除成功',url('gift/music_index'));
        }else{
            $this->error_new('删除失败');
        }
    }

    /**
     * 编辑音乐
     */
    public function music_edit(Request $request){
        if($request->isPost()){
            $data = $request->post();
            $map=array(
                'id'=>$data['id']
            );
            $result=Db::name('music')->where($map)->update($data);
            if ($result) {
                $this->success_new('修改成功',url('gift/music_index'));
            }else{
                $this->error_new('修改失败');
            }
        }else{
            $id           = $request->param('id');
            $data         = Db::name('music')->where(array('id'=>$id))->find();
            $qiniu_config = config('qiniu');
            $accessKey    = $qiniu_config['ACCESSKEY'];
            $secretKey    = $qiniu_config['SECRETKEY'];
            $auth         = new Auth($accessKey, $secretKey);
            $token        = $auth->uploadToken($qiniu_config['bucket']);
            $domain       = $qiniu_config['domain'];
            return view('',[
                'data'=>$data,
                'token'=>$token,
                'domain'=>$domain
            ]);
        }
    }

    /**
     * 添加音乐
     */
    public function music_add(Request $request){
        if($request->isPost()){
            $data    = $request->post();
            $result=Db::name('music')->insert($data);
            if ($result) {
                $this->success_new('添加成功',url('gift/music_index'));
            }else{
                $this->error_new('添加失败');
            }
        }else{
            $qiniu_config = config('qiniu');
            $accessKey    = $qiniu_config['ACCESSKEY'];
            $secretKey    = $qiniu_config['SECRETKEY'];
            $auth         = new Auth($accessKey, $secretKey);
            $token        = $auth->uploadToken($qiniu_config['bucket']);
            $domain        = $qiniu_config['domain'];
            return view('',[
                'token'=>$token,
                'domain'=>$domain
            ]);
        }
    }


}